#ifndef __NONAME_CRICULARQUEUE_H__
#define __NONAME_CRICULARQUEUE_H__

#define NONAME_LIB_USE

#include <ntifs.h>
#include "nm_mem.h"

#define DEFAULT_MAX_CQ_SIZE 64

template <class T>
class cirbuf
{
public:
	cirbuf( ULONG Size = DEFAULT_MAX_CQ_SIZE ) : m_Tail(0), m_Head(0), m_ItemCount(0)
	{
		m_Buffer = new T[Size];
		m_MaxSize = Size;
	}

	void Release()
	{
		delete[] m_Buffer;
		m_MaxSize = 0;
	}

public:
	void Push( T &val ) {

		m_Buffer[m_Tail] = val;
		if ((m_Tail == m_Head) && (m_ItemCount != 0)) {
			m_Head = ( (m_Head + 1) % m_MaxSize );
		}
		m_Tail = ( (m_Tail + 1) % m_MaxSize );
		if (m_ItemCount != m_MaxSize) m_ItemCount++;
	}

	void Pop( T &val ) {

		val = m_Buffer[m_Head];
		m_Head = ( (m_Head + 1) % m_MaxSize );
		m_ItemCount--;
	}

	bool IsEmpty() { return (m_ItemCount == 0); }
	ULONG GetCurSize() { return m_ItemCount; }

private:
	ULONG m_Head;
	ULONG m_Tail;
	ULONG m_ItemCount;
	ULONG m_MaxSize;
	T *m_Buffer;
};

#endif